package com.wh1200.mybatis.xmlless.config

import com.wh1200.mybatis.xmlless.model.Properties
import com.wh1200.mybatis.xmlless.resolver.ColumnsResolver
import com.wh1200.mybatis.xmlless.util.FieldUtil
import com.wh1200.xmlless.test.dao.DogDAO
import com.wh1200.xmlless.test.model.Dog
import com.wh1200.xmlless.test.model.Student
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaMethod
import kotlin.test.assertContains


/**
 *
 * Created by 吴昊 on 2018-12-09.
 *
 * @author 吴昊
 * @since 0.0.1
 */
class DogDAOResolverTest : BaseResolverTest(
    DogDAO::class.java
) {

  @Test
  @DisplayName("测试判断字段是可插入和可更新状态")
  fun resolve2() {
    Student::class.java.declaredFields.forEach {
      if (it == Student::scores.javaField) {
        assert(FieldUtil.isInsertIgnore(it))
        assert(FieldUtil.isUpdateIgnore(it))
      }
      if (it == Student::createTime.javaField) {
        assert(!FieldUtil.isInsertIgnore(it))
        assert(FieldUtil.isUpdateIgnore(it))
      }
    }
  }

  @Test
  @DisplayName("解析持久化类的字段映射")
  fun resolveColumns() {
    val methodInfo = createMethodInfo(DogDAO::findById.javaMethod!!)
    val cols = ColumnsResolver.resolveSelectColumns(Properties(), methodInfo).map {
      it.toSql()
    }
    assertContains(cols, "t_dog.ages AS ages")
    assertContains(cols, "t_dog.delete_flag AS deleteFlag")
    assertContains(cols, "t_dog.create_time AS createTime")
  }

}
